package hhs.poc.activities.whoiswho;

import hhs.poc.util.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

import android.R;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class List extends Activity {
	ScrollView sv;
	LinearLayout main;
	RelativeLayout rl;
	TextView tv_dayOfWeek;
	TextView tv_date;
	TextView tv_title;
	String room_nr;
	ArrayList<Appointment> appointments;
	HashMap<String, String> daysOfWeek;
	Locale locale;
	String dayOfWeek;
	String name;
	String act_type;
	DatabaseManager db;
	Employee current_emp;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		db = new DatabaseManager(this);
		current_emp = utils.retrieveEmployee();

		sv = new ScrollView(this); // on screen
		main = new LinearLayout(this); // on sv
		rl = new RelativeLayout(this); // on main
		tv_dayOfWeek = new TextView(this); // on rl
		tv_date = new TextView(this); // on rl
		tv_title = new TextView(this); // on rl
		daysOfWeek = new HashMap<String, String>();
		locale = new Locale("nl"); // NL || nl || nl_NL || nl_BE
		dayOfWeek = new SimpleDateFormat("E", locale).format(new Date());

		daysOfWeek.put("ma", "Maandag");
		daysOfWeek.put("di", "Dinsdag");
		daysOfWeek.put("wo", "Woensdag");
		daysOfWeek.put("do", "Donderdag");
		daysOfWeek.put("vr", "Vrijdag");
		daysOfWeek.put("za", "Zaterdag");
		daysOfWeek.put("zo", "Zondag");

		Bundle extras = getIntent().getExtras();
		room_nr = extras.getString("room_nr");
		act_type = extras.getString("act_type");

		initComponents();
	}

	private void initComponents() {
		utils.setTitleBackgroundColor(findViewById(android.R.id.title));
		
		dayOfWeek = daysOfWeek.get(dayOfWeek);
		String date = new SimpleDateFormat("dd-MM-yyyy", locale)
				.format(new Date());

		tv_dayOfWeek.setLayoutParams(new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		tv_dayOfWeek.setPadding(5, 0, 5, 0);
		tv_dayOfWeek.setTextColor(Color.BLACK);
		tv_dayOfWeek.setText(dayOfWeek);
		tv_dayOfWeek.setTypeface(null, Typeface.BOLD);
		tv_dayOfWeek.setId(1);
		RelativeLayout.LayoutParams dayOfWeek_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		dayOfWeek_params.addRule(RelativeLayout.CENTER_HORIZONTAL);

		tv_date.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		tv_date.setPadding(5, 0, 5, 0);
		tv_date.setTextColor(Color.BLACK);
		tv_date.setText(date);
		tv_date.setId(2);
		RelativeLayout.LayoutParams date_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		date_params.addRule(RelativeLayout.BELOW, tv_dayOfWeek.getId());
		date_params.addRule(RelativeLayout.CENTER_HORIZONTAL);

		tv_title.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		tv_title.setPadding(5, 10, 5, 0);
		tv_title.setTextColor(Color.BLACK);
		if (act_type.equals("schedule")) {
			setTitle("Rooster");
			name = current_emp.getName();
			tv_title.setText(name);
			appointments = db.getAppointmentsByEmp(current_emp.getId(), date);
		} else if (act_type.equals("reservation")) {
			setTitle("Reserveringen");
			tv_title.setText(room_nr);
			appointments = db.getAppointmentsByRoom(room_nr, date);
		}
		RelativeLayout.LayoutParams room_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		room_params.addRule(RelativeLayout.BELOW, tv_date.getId());
		room_params.addRule(RelativeLayout.CENTER_HORIZONTAL);

		rl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.WRAP_CONTENT));
		rl.setBackgroundColor(Color.WHITE);

		rl.addView(tv_dayOfWeek, dayOfWeek_params);
		rl.addView(tv_date, date_params);
		rl.addView(tv_title, room_params);

		main.setOrientation(1);
		main.setBackgroundColor(Color.WHITE);
		main.addView(rl);

		for (Appointment ap : appointments) {
			// Employee emp = db.getEmployee(ap.getEmp_id());
			String[] emp_names = db.getAppEmps(ap.getId());
			View view = createAppointment(ap.getDate(), ap.getBegin(),
					ap.getEnd(), ap.getLesson(), ap.getGroup(), ap.getRoom(),
					emp_names);
			main.addView(view);
		}

		sv.addView(main);
		setContentView(sv);
	}

	public View createAppointment(String date, String begin, String end,
			String lesson, String group, String room, String[] occupier) {
		TextView l = new TextView(this);
		l.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		l.setText(lesson);
		l.setPadding(5, 0, 5, 0);
		l.setTextColor(Color.BLACK);
		l.setId(1);

		TextView r = new TextView(this);
		r.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		r.setText(room);
		r.setPadding(5, 0, 5, 0);
		r.setTextColor(Color.BLACK);
		r.setId(2);
		RelativeLayout.LayoutParams room_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		room_params.addRule(RelativeLayout.BELOW, l.getId());

		TextView t = new TextView(this);
		t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		t.setText(begin + "-" + end);
		t.setPadding(5, 0, 5, 0);
		t.setTextColor(Color.BLACK);
		t.setId(3);
		RelativeLayout.LayoutParams time_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		time_params.addRule(RelativeLayout.BELOW, l.getId());
		time_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		TextView g = new TextView(this);
		g.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		g.setText(group);
		g.setPadding(5, 0, 5, 0);
		g.setTextColor(Color.BLACK);
		RelativeLayout.LayoutParams group_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		group_params.addRule(RelativeLayout.BELOW, t.getId());

		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(1);
		ll.setGravity(Gravity.RIGHT);

		RelativeLayout.LayoutParams ll_params = new RelativeLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		ll_params.addRule(RelativeLayout.BELOW, t.getId());
		ll_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

		for (String occ : occupier) {
			TextView o = new TextView(this);
			o.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			o.setText(occ);
			o.setPadding(5, 0, 5, 0);
			o.setTextColor(Color.BLACK);

			ll.addView(o);
		}

		FrameLayout border = new FrameLayout(this);
		border.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, 200));
		border.setPadding(0, 5, 0, 5);
		border.setBackgroundColor(Color.BLACK);

		RelativeLayout appointment = new RelativeLayout(this);
		appointment.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT));
		appointment.setBackgroundColor(Color.parseColor("#eeeeee"));

		appointment.addView(l);
		appointment.addView(r, room_params);
		appointment.addView(t, time_params);
		appointment.addView(g, group_params);
		appointment.addView(ll, ll_params);

		border.addView(appointment);

		return border;
	}
}
